package com.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.pojo.Member;
import com.wms.service.impl.MemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Description:
 * @Project:
 * @CreateDate: Created in 2020/3/20 20:17
 * @Author: YXH
 */
@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    MemberServiceImpl memberService;

    @RequestMapping("/allMember")
    public String allMember(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize,
                            Model model){
        PageHelper.startPage(pageNum,pageSize);
        List<Member> allMember = memberService.allMember();
        PageInfo memberPage = new PageInfo(allMember);
        model.addAttribute("memberPage",memberPage);
        return "member/member-list";
    }


    @RequestMapping("/toAdd")
    public String toAdd(){ return "/member/member-add";
    }

    @RequestMapping("/addMember")
    public String addMember(Member member){
        memberService.addMember(member);
        return "redirect:/member/allMember";
    }

    @RequestMapping("/toEdit")
    public String toEdit(@RequestParam(name = "uid")String uid,
                         Model model){
        int id = Integer.parseInt(uid);
        Member member = memberService.queryById(id);
        model.addAttribute("member",member);
        return "/member/member-edit";
    }

    @RequestMapping("/editMember")
    public String editMember(Member member){
        System.out.println(member);
        memberService.editMember(member);
        return "redirect:/member/allMember";
    }

    @RequestMapping("/delMember")
    public String delMember(@RequestParam(name = "uid")String uid){
        int id = Integer.parseInt(uid);
        memberService.delMember(id);
        return "redirect:/member/allMember";
    }
}
